package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 分页加条件查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }

    /**
     * 添加角色
     * @param role
     * @param menus
     * @param permissions
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] menus, Integer[] permissions){
        try {
            roleService.add(role,menus,permissions);
            return new Result(true,"添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色信息失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){

        boolean b = roleService.deleteByid(id);
        if(b){
            return new Result(true,"删除成功");
        }
        return new Result(false,"删除失败，该角色有用户在引用");

    }
}
